package com.hjm.project.incomeInfo.controller;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.job.task.domain.JobTask;
import com.hjm.project.job.task.service.IJobTaskService;
import com.hjm.project.terminal.manufacturer.domain.Manufacturer;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.incomeInfo.domain.IncomeInfo;
import com.hjm.project.incomeInfo.service.IIncomeInfoService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;

import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 收益统计Controller
 * 
 * @author maker-platform
 * @date 2024-03-13
 */
@Controller
@RequestMapping("/incomeInfo")
public class IncomeInfoController extends BaseController {
    private String prefix = "incomeInfo";

    @Resource
    private IIncomeInfoService incomeInfoService;
    @Resource
    private IManufacturerService manufacturerService;
    @Resource
    private IJobTaskService jobTaskService;

    @RequiresPermissions("incomeInfo:incomeInfo:view")
    @GetMapping()
    public String incomeInfo() {
        return prefix + "/incomeInfo";
    }

    /**
     * 查询收益统计列表
     */
    @RequiresPermissions("incomeInfo:incomeInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IncomeInfo incomeInfo) {
        startPage();
        List<IncomeInfo> list = incomeInfoService.selectIncomeInfoList(incomeInfo);
        return getDataTable(list);
    }

    /**
     * 导出收益统计列表
     */
    @RequiresPermissions("incomeInfo:incomeInfo:export")
    @Log(title = "收益统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IncomeInfo incomeInfo) throws Exception {
        CompletableFuture<JobTask> cbf = incomeInfoService.exportBb(incomeInfo);
        jobTaskService.asyncUploadFileFuture(cbf);
        return success("请前往综合管理-下载队列下载");
    }

    /**
     * 新增收益统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存收益统计
     */
    @RequiresPermissions("incomeInfo:incomeInfo:add")
    @Log(title = "收益统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IncomeInfo incomeInfo) {
        return toAjax(incomeInfoService.insertIncomeInfo(incomeInfo));
    }

    /**
     * 修改收益统计
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        IncomeInfo incomeInfo = incomeInfoService.selectIncomeInfoById(id);
        mmap.put("incomeInfo", incomeInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存收益统计
     */
    @RequiresPermissions("incomeInfo:incomeInfo:edit")
    @Log(title = "收益统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IncomeInfo incomeInfo) {
        return toAjax(incomeInfoService.updateIncomeInfo(incomeInfo));
    }

    /**
     * 删除收益统计
     */
    @RequiresPermissions("incomeInfo:incomeInfo:remove")
    @Log(title = "收益统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(incomeInfoService.deleteIncomeInfoByIds(ids));
    }
    @Log(title = "收益统计", businessType = BusinessType.INSERT)
    @PostMapping( "/addICome")
    @ResponseBody
    public AjaxResult addICome(IncomeInfo incomeInfo) {
        List<Manufacturer> manufacturers = manufacturerService.selectManufacturerList(null);
        for (Manufacturer manufacturer : manufacturers) {
            IncomeInfo info = new IncomeInfo();
            info.setStatus("0");
            info.setCreateDate(DateUtils.getDate());
            info.setTradeDate(incomeInfo.getTradeDate());
            info.setAddMan(ShiroUtils.getLoginName());
            info.setManuNum(manufacturer.getManuNum().toString());
            info.setManuName(manufacturer.getManuName());
            info.setNote("正在生成......");
            incomeInfoService.deleteIncomeInfo(info);
            incomeInfoService.insertIncomeInfo(info);
        }
        incomeInfoService.selectCount(ShiroUtils.getLoginName(),incomeInfo.getTradeDate(),manufacturers);
        return AjaxResult.success("报表生成中,请稍后进行下载");
    }
}
